package com.yang.springcloud.dao;

import com.yang.springcloud.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class StudentImp implements StudentDao {

    @Autowired
    DataSource dataSource;
    Connection conn;
    PreparedStatement pstmt;

    public Map<String, Object> addStudent(Student s) {

        int i;
        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("insert into student (s_id,s_name,s_birth,s_sex) values(?,?,?,?)");
            pstmt.setString(1, s.getId());
            pstmt.setString(2, s.getName());
            pstmt.setString(3, s.getBirth());
            pstmt.setString(4, s.getSex());
            i = pstmt.executeUpdate();
            System.out.println(i);

            pstmt.close();
            conn.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> delereStudent(String name) {
        int i;
        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("delete from student where s_name= ?");
            pstmt.setString(1, name);

            i = pstmt.executeUpdate();
            System.out.println(i);

            pstmt.close();
            conn.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "删除成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> updateStudent(Student s) {
        int i;
        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("update student set s_name=?, s_birth=?, s_sex=? where s_id=?");
            pstmt.setString(1, s.getName());
            pstmt.setString(2, s.getBirth());
            pstmt.setString(3, s.getSex());
            pstmt.setString(4, s.getId());
            i = pstmt.executeUpdate();
            System.out.println(i);

            pstmt.close();
            conn.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "修改成功");
        msg.put("code", 200);
        return msg;
    }

    public List<Student> queryAll() {
        ResultSet rs;
        List<Student> stuudentList = new ArrayList<>();

        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM student");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String id = rs.getString("s_id");
                String name = rs.getString("s_name");
                String birth = rs.getString("s_birth");
                String sex = rs.getString("s_sex");
                Student s = new Student(id, name, birth, sex);
                stuudentList.add(s);
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return stuudentList;
    }

}
